package com.nf.teamwork.mall.controller.login;

import com.nf.teamwork.mall.entity.UserInf;
import com.nf.teamwork.mall.service.UserInfService;
import com.nf.teamwork.mall.vo.ResponseVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: LJP
 * @Classname LoginController
 * @Date: 2020-02-27 19:25
 * @Description:
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    /*@Secured("ROLE_ADMIN")
    @GetMapping("/index")
    public ResponseVO index(){
        return new ResponseVO(200, "hello", "index");
    }

    @Secured({"ROLE_ADMIN", "ROLE_USER"})
    @GetMapping("/index2")
    public ResponseVO index2(){
        return new ResponseVO(200, "hello2", "index2");
    }

    @Secured({"ROLE_ADMIN", "ROLE_PRODUCT"})
    @GetMapping("/index3")
    public ResponseVO index3(){
        return new ResponseVO(200, "hello3", "index3");
    }
    @GetMapping("/userInf")
    public String getUserInf(Principal principal){
        String name = principal.getName();
        return name;
    }*/

    @Autowired
    private UserInfService userInfService;

    @ApiOperation(value = "注册新用户", notes = "只需要传入邮箱、昵称、密码，会返回生成的账号 data.data调用即可取到数据")
    @PostMapping("/register")
    public ResponseVO register(@ApiParam("用户对象（只需要：userInfEmail、userInfNickname、password）") @RequestBody UserInf userInf){
        boolean result = userInfService.register(userInf);
        return ResponseVO.newBuilder()
                .code(result ? 200 : 500)
                .message(result ? "注册成功" : "注册失败")
                //这里将系统为用户生成的帐号响应回去
                .data(userInf.getUsername())
                .build();
    }

    @ApiOperation(value = "账号激活", notes = "将传入用户对象的账号的激活状态更新为激活")
    @PatchMapping("/activate")
    public ResponseVO activate(@ApiParam("用户对象（只需要：username(登录账号)、emailCode）") @RequestBody UserInf userInf){
        boolean result = userInfService.activate(userInf);
        return ResponseVO.newBuilder()
                .code(result ? 200 : 500)
                .message(result ? "账号激活成功" : "账号激活失败")
                .data(userInf)
                .build();
    }
}
